//
//  InteractionViewController.m
//  WuLi
//
//  Created by Gollum on 16/8/29.
//  Copyright © 2016年 Gollum. All rights reserved.
//

#import "InteractionViewController.h"
#import "TopScrollMenu.h"
#import "ProphesyViewController.h"
#import "TestViewController.h"
@interface InteractionViewController ()<TopScrollMenuDelegate,UIScrollViewDelegate>
@property (nonatomic, strong) TopScrollMenu *topScrollMenu;
@property (nonatomic, strong) UIScrollView *mainScrollView;
@property (nonatomic, strong) NSArray *titles;
@end

@implementation InteractionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
//    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // 1.添加所有子控制器
    [self setupChildViewController];
    
    self.titles = @[@"预言帝", @"测试咖"];
    
    self.topScrollMenu = [TopScrollMenu topScrollMenuWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];
    
    //滚动启用
    //    _topScrollMenu.scrollTitleArr = [NSArray arrayWithArray:_titles];
    
    _topScrollMenu.staticTitleArr = [NSArray arrayWithArray:_titles];
    
    _topScrollMenu.topScrollMenuDelegate = self;
    [self.view addSubview:_topScrollMenu];
    
    // 创建底部滚动视图
    self.mainScrollView = [[UIScrollView alloc] init];
    _mainScrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    _mainScrollView.contentSize = CGSizeMake(self.view.frame.size.width * _titles.count, 0);
    _mainScrollView.backgroundColor = [UIColor clearColor];
    // 开启分页
    _mainScrollView.pagingEnabled = YES;
    // 没有弹簧效果
    _mainScrollView.bounces = NO;
    // 隐藏水平滚动条
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    // 设置代理
    _mainScrollView.delegate = self;
    [self.view addSubview:_mainScrollView];
    
    [self showVc:0];
    
    [self.view insertSubview:_mainScrollView belowSubview:_topScrollMenu];
}
#pragma mark - - - TopScrollMenu代理方法
- (void)TopScrollMenu:(TopScrollMenu *)topScrollMenu didSelectTitleAtIndex:(NSInteger)index{
    
    // 1 计算滚动的位置
    CGFloat offsetX = index * self.view.frame.size.width;
    self.mainScrollView.contentOffset = CGPointMake(offsetX, 0);
    
    // 2.给对应位置添加对应子控制器
    [self showVc:index];
}

- (void)setupChildViewController {
    // 精选
    ProphesyViewController *prophesyVC = [[ProphesyViewController alloc] init];
    [self addChildViewController:prophesyVC];
    
    // 电视剧
    TestViewController *testVC = [[TestViewController alloc] init];
    [self addChildViewController:testVC];
    
 
    
}

// 显示控制器的view
- (void)showVc:(NSInteger)index {
    
    CGFloat offsetX = index * self.view.frame.size.width;
    
    UIViewController *vc = self.childViewControllers[index];
    
    // 判断控制器的view有没有加载过,如果已经加载过,就不需要加载
    if (vc.isViewLoaded) return;
    
    [self.mainScrollView addSubview:vc.view];
    
    vc.view.frame = CGRectMake(offsetX, 0, self.view.frame.size.width, self.view.frame.size.height);
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    // 计算滚动到哪一页
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    
    // 1.添加子控制器view
    [self showVc:index];
    
    // 2.把对应的标题选中
    UILabel *selLabel = self.topScrollMenu.allTitleLabel[index];
    
    [self.topScrollMenu staticTitleLabelSelecteded:selLabel];
    
    //滚动启用
    //  [self.topScrollMenu scrollTitleLabelSelecteded:selLabel];
    
    // 3.让选中的标题居中
    //[self.topScrollMenu scrollTitleLabelSelectededCenter:selLabel];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
